// pages/schoolxq/schoolxq.js
const app = getApp()
Page({

 /**
   * 页面的初始数据
   */
  data: {
    circleid: 0,
    listArr: [],
    comment_list: 0,
    userId: -1,
    input:'',
    user:{}
  },
  
  selectComment(){
    wx.request({
      url: 'https://www.woodymidnight.cn/BCUHelper/api/comment/selectbytypeandid',
      method: 'POST',
      header: { 
        'content-type': 'application/x-www-form-urlencoded' 
      }, 
      data: {
        comType: 0,
        comObjId: this.data.listArr.circleid
      },
      success: res =>{
        console.log(res);
        this.setData({
          comment_list: res.data.data
        })
      }
    })
  },

  chaxun(){
      var that=this;
    wx.request({  
      url: 'https://www.woodymidnight.cn/BCUHelper/api/circle/selectbycircleid',  
      method: 'POST',
      data:{
          circleId : this.data.circleid
      },                                      
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      success: function (res) {   
        var resData = res.data;
        console.log("数据" , resData);
        resData.data.circletime = app.toData(resData.data.circletime)
        that.setData({
          listArr: resData.data
        })
        that.selectComment()
      }
    })
    },
    addlike(e){
      var that = this;
      wx.request({
        url: 'https://www.woodymidnight.cn/BCUHelper/api/circle/updatacirclelike',
        method: 'POST',
        data:{
          circleId : this.data.circleid
        },
        header: { 'content-type': 'application/x-www-form-urlencoded' },
        success: function (res) {   
          var resData = res.data;
          console.log("数据" , resData);
          console.log("this:",this);
          that.setData({
            listArr: resData.data
          })
        }
      })
    },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      console.log(options);
      var  circleid = parseInt(options. circleid);
      this.setData({
        circleid :  circleid,
        userId: app.data.userInfo.userid,
        user: app.data.userInfo
      })
      this.chaxun();
  },

  del: function(e) {
    var a = this;
    console.log(e.currentTarget.dataset.id), 
    wx.showModal({
        content: "确认删除该评论吗？",
        success: res => {
          if(res.confirm){
            wx.request({
              url: 'https://www.woodymidnight.cn/BCUHelper/api/comment/delbycomid',
              method: 'POST',
              data:{
                comId : e.currentTarget.dataset.id
              },
              header: { 'content-type': 'application/x-www-form-urlencoded' },
              success: res => {
                a.selectComment()
              }
            })
          }else if (res.cancel) {

          }
        }
    });
  },

  input: function(res){
    this.setData({
      input: res.detail.value
    })
  },

  submit: function(res){
    // console.log(this.data.circleid);
    wx.request({
      url: 'https://www.woodymidnight.cn/BCUHelper/api/comment/insert',
      method: 'POST',
      data: {
        comcontent: this.data.input,
        comobjid: this.data.circleid,
        comtype: 0,
        comuserid: this.data.user.userid,
        comuserimage: this.data.user.userimage,
        comusername: this.data.user.username
      },
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      success: res =>{
        console.log(res);
        this.setData({
          input: ''
        })
        this.selectComment()
      }
    })
  }

})